/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Visual;

import Logica.Empleado;
import Logica.Taller;
import Persistencia.exceptions.NonexistentEntityException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Pablo
 */
public class TallerEmpleado extends javax.swing.JInternalFrame {

    ControladoraVisual miVisual;
    double dni = 0;
    DefaultComboBoxModel modeloTalleres = new DefaultComboBoxModel();

    /**
     * Creates new form TallerEmpleado
     */
    public TallerEmpleado(ControladoraVisual miVisual, double dni) {
        initComponents();
        this.miVisual = miVisual;
        this.dni = dni;
        this.cmbTalleres.setModel(modeloTalleres);
        this.cargarTalleres();
        this.cargarCampos();
    }

    public void cargarTalleres() {
        List<Taller> talleres = miVisual.dameTalleres();
        for (Taller miTaller : talleres) {
            this.modeloTalleres.addElement(miTaller);
        }
    }

    public void cargarCampos() {

        Empleado miEmpleado = miVisual.dameEmpleado(this.dni);
        String campo = miEmpleado.getNombre() + " " + miEmpleado.getApellido();
        this.txtEmpleado.setText(campo);
        this.txtEmpleado.setEditable(false);


    }
    public void referenciarEmpleado() throws NonexistentEntityException, Exception{  // AGREGO UNA REFERENCIA DEL EMPLEADO EN SU TALLER CORRESPONDIENTE
        
        Empleado miEmpleado = miVisual.dameEmpleado(this.dni);
        Taller unTaller = (Taller) this.cmbTalleres.getSelectedItem();
        int nroTaller = unTaller.getNroTaller();
        List<Taller> talleres = miVisual.dameTalleres();
        for(Taller miTaller:talleres){
            if(miTaller.getNroTaller() == unTaller.getNroTaller()){
                List<Empleado> empleados = miTaller.getEmpleados();
                empleados.add(miEmpleado);
                unTaller.setEmpleados(empleados);
                miVisual.editarTaller(unTaller);
            }
        }
        
        
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtEmpleado = new javax.swing.JTextField();
        txtLocalidad = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cmbTalleres = new javax.swing.JComboBox();
        txtSalir = new javax.swing.JButton();
        txtAsignarTaller = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("ASIGNACION DE TALLER");

        jLabel2.setText("Empleado");

        jLabel3.setText("Localidad");

        jLabel4.setText("TALLER");

        txtSalir.setText("Salir");
        txtSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtSalirActionPerformed(evt);
            }
        });

        txtAsignarTaller.setText("Asignar Taller");
        txtAsignarTaller.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAsignarTallerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txtEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtLocalidad)
                                    .addComponent(cmbTalleres, 0, 168, Short.MAX_VALUE))))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtAsignarTaller)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(txtSalir)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtEmpleado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtLocalidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cmbTalleres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 54, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSalir)
                    .addComponent(txtAsignarTaller))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtAsignarTallerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAsignarTallerActionPerformed
        Empleado miEmpleado = miVisual.dameEmpleado(this.dni);
        Taller unTaller = (Taller) this.cmbTalleres.getSelectedItem();
        miEmpleado.setUnTaller(unTaller);
        try {
            miVisual.editarEmpleado(miEmpleado);
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TallerEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TallerEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        }
       /*
         try {
            this.referenciarEmpleado();
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(TallerEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(TallerEmpleado.class.getName()).log(Level.SEVERE, null, ex);
        }
        */
    }//GEN-LAST:event_txtAsignarTallerActionPerformed

    private void txtSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSalirActionPerformed
        this.dispose();
    }//GEN-LAST:event_txtSalirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbTalleres;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton txtAsignarTaller;
    private javax.swing.JTextField txtEmpleado;
    private javax.swing.JTextField txtLocalidad;
    private javax.swing.JButton txtSalir;
    // End of variables declaration//GEN-END:variables
}
